package com.rqf.mr.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.rqf.mr.service.IBedService;

@Service("BedService")
public class BedService implements IBedService {

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public List<Map<String, Object>> getBedList(String nurseLoginId,String whereStr) {
		if (StringUtils.isEmpty(nurseLoginId)) {
			return null;
		}
		String sql = "Select * from view_bed_patiinfo where ( NURSEEMPID = (Select EMPID from view_ehisuser where LOGINNAME = '"+nurseLoginId+"')) AND ("
				+ whereStr+")";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}
	
	@Override
	public List<Map<String, Object>> getNurseBedList(String nurseLoginId) {
		if (StringUtils.isEmpty(nurseLoginId)) {
			return null;
		}
		String sql = "Select * from view_bed_patiinfo where NURSEEMPID = (Select EMPID from view_ehisuser where LOGINNAME = '"+nurseLoginId+"')";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public void insertBedinfo(Map<String, Object> bedInfo) {
		// TODO Auto-generated method stub

	}

	@Override
	public void deleteBedinfo(String bedId) {
		// TODO Auto-generated method stub

	}

	@Override
	public Boolean updateBedInfo(Map<String, Object> bedInfo) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean updateTeamedBedInfo(List<Map<String, Object>> bedInfoList) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Map<String, Object>> getTeamedBedInfoByNurseId(
			String nurseLoginId,String page) {
		if (StringUtils.isEmpty(nurseLoginId)) {
			return null;
		}
		String whereStr = "";
		if (!StringUtils.isEmpty(page)) {
			whereStr = "and rownum < 15 * " + Integer.parseInt(page);
		}
		String sql = "select * from view_bed_patiinfo   bbb where bbb.ORGID_BELONG =(select n.teamnum from nurse n where n.nurse_loginid ='"
				+ nurseLoginId + "')" + whereStr;
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public List<Map<String, Object>> getbqBedInfoByNurseId(String nurseLoginId,
			String page, String which) {
		if (StringUtils.isEmpty(nurseLoginId) || StringUtils.isEmpty(page)
				|| StringUtils.isEmpty(which)) {
			return null;
		}
		String sql = "";
		if ("no".equals(page)) {
			sql = "select '' bed_id, '' addflag, cc.BEDDES, cc.BEDID,'' inpatiid, cc.ORGID_BELONG, cc.OUTFLAG, '' pati_age, '' pati_bed_id, '' pati_name,rownum RN  from view_bed_patiinfo cc where cc.BEDDES like '"
					+ which + "%'  and cc.ORGID_BELONG=(select uu.orgid  from view_ehisuser uu  where uu.loginname = '" + nurseLoginId + "' )";
		} else if ("byinpatiid".equals(page)) {
			sql = "select '' bed_id, '' addflag, cc.BEDDES, cc.BEDID,inpatiid, cc.ORGID_BELONG, cc.OUTFLAG, '' pati_age, '' pati_bed_id, '' pati_name,rownum RN  from view_bed_patiinfo cc where cc.INPATIID like '"
					+ which + "'  and cc.ORGID_BELONG=(select uu.orgid  from view_ehisuser uu  where uu.loginname = '" + nurseLoginId + "' )";
		} else {
			sql = "select *  from (select aa.*,ROWNUM RN from (select '' bed_id, '' addflag, cc.BEDDES, cc.BEDID,'' inpatiid, cc.ORGID_BELONG, cc.OUTFLAG, '' pati_age, '' pati_bed_id, '' pati_name   from view_bed_patiinfo cc  where cc.ORGID_BELONG = (select uu.orgid  from view_ehisuser uu  where uu.loginname = '"
					+ nurseLoginId + "')) aa  where rownum < 15 * " + Integer.parseInt(page) + " ) where RN > 15*" + (Integer.parseInt(page) - 1) + "";
		}
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		return list;
	}

	@Override
	public Map<String, Object> getBedDetailByInpatiId(String InpatiId) {
		if (StringUtils.isEmpty(InpatiId)) {
			return null;
		}
		String sql = "Select * from view_bed_patiinfo where INPATIID = '"+InpatiId+"'";
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sql);
		SimpleDateFormat time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 
		for (int i = 0; i < list.size(); i++) {
			list.get(i).put("BIRTHDATE", time.format((Date)list.get(i).get("BIRTHDATE")));
			list.get(i).put("ADMITDATE", time.format((Date)list.get(i).get("ADMITDATE")));
		}
		if (list.size()>0) {
			return list.get(0);
		}else {
			return null;
		}
	}
}
